﻿#include "Windows.h"
#include "Commdlg.h"
#include "zyFontDialog.h"


BlackMoonUI::zyFontDialog::zyFontDialog()
{
	SetMember(L"this", (LONG_PTR)this);
	m_obj->setWndProc(zyFontDialog_WndProc);
}
BlackMoonUI::zyFontDialog::~zyFontDialog()
{
	CHOOSEFONT* cc = (CHOOSEFONT*)GetMember(L"pChooseFont");
	if (cc != nullptr) {
		delete cc;
	}
}
LRESULT WINAPI BlackMoonUI::zyFontDialog::zyFontDialog_WndProc(zyObject* obj, HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL* bHandle)
{
	LRESULT ret = zyForm_WndProc(obj, hWnd, uMsg, wParam, lParam, bHandle);
	if (ret) return ret;
	if (uMsg == WM_COMMAND) {
		int id = LOWORD(lParam);
		int code = HIWORD(lParam);
		if (id == 1026 && code == BN_CLICKED) {
			TRegEvent* p = obj->getRegEvent(L"OnApply");
			if (p) {
				p->handler<int>(lParam, id);
			}
		}
	}
	return 0;
}
LRESULT CALLBACK BlackMoonUI::zyFontDialog::CFHookProc(HWND hdlg, UINT message, WPARAM wParam, LPARAM lParam) {
	if (message == WM_INITDIALOG) {
		CHOOSEFONT* cc = reinterpret_cast<CHOOSEFONT*>(lParam);
		zyFontDialog* Dialog = reinterpret_cast<zyFontDialog*>(cc->lCustData);
		if (Dialog) {
			Dialog->SetMember(L"hWnd", (LONG_PTR)hdlg);
			//窗口数据绑定[hdlg] = Dialog;
			zyEnv::getInstance().registerWnd(hdlg, Dialog->getObject());
			Dialog->_MoveToStartPosition(hdlg);
		}
	}
	//zyBase* this_ = 窗口数据绑定[hdlg];// 如果找到了对应的 zyBase 实例，则尝试处理消息
	if (zyObject* obj = zyEnv::getInstance().getWndObject(hdlg)) {
		BOOL bHand = false;
		LRESULT ret = obj->callWndProc(hdlg, message, wParam, lParam, &bHand);
		if (bHand) return ret;
	}
	return 0;
}
bool BlackMoonUI::zyFontDialog::打开(HWND 参数_父窗口)
{
	CHOOSEFONT* pChooseFont = (CHOOSEFONT*)GetMember(L"pChooseFont");
	if (pChooseFont == nullptr) {
		return false;
	}
	pChooseFont->hwndOwner = 参数_父窗口;
	return ChooseFont(pChooseFont) != 0;
}
void BlackMoonUI::zyFontDialog::初始化(int 参数_选项, LPLOGFONTW 参数_字体, int 参数_初始颜色, int 参数_最小尺寸, int 参数_最大尺寸) {
	CHOOSEFONT* cf = (CHOOSEFONT*)GetMember(L"pChooseFont");
	if (cf != nullptr) {
		delete cf;
	}
	cf = new CHOOSEFONT;
	ZeroMemory(cf, sizeof(CHOOSEFONT)); // 初始化结构体，避免未定义行为

	cf->lStructSize = sizeof(CHOOSEFONT);
	cf->Flags = 参数_选项 | CF_ENABLEHOOK;
	cf->lpfnHook = (LPCFHOOKPROC)BlackMoonUI::zyFontDialog::CFHookProc;
	cf->lCustData = reinterpret_cast<LPARAM>(this);
	cf->rgbColors = 参数_初始颜色;
	cf->nSizeMin = 参数_最小尺寸;
	cf->nSizeMax = 参数_最大尺寸;
	cf->Flags |= CF_INITTOLOGFONTSTRUCT;
	cf->lpLogFont = 参数_字体;

	SetMember(L"pChooseFont", (LONG_PTR)cf);
}
LPLOGFONTW BlackMoonUI::zyFontDialog::取字体() {
	CHOOSEFONT* pChooseFont = (CHOOSEFONT*)GetMember(L"pChooseFont");
	if (pChooseFont == nullptr) {
		return nullptr;
	}
	return pChooseFont->lpLogFont;
}
std::wstring BlackMoonUI::zyFontDialog::取字体名称() {
	CHOOSEFONT* pChooseFont = (CHOOSEFONT*)GetMember(L"pChooseFont");
	if (pChooseFont == nullptr) {
		return L"";
	}
	return std::wstring(pChooseFont->lpLogFont->lfFaceName);
}
LOGFONT* BlackMoonUI::zyFontDialog::取字体应用() {
	HWND hWnd = 取窗口句柄();
	if (hWnd == 0) {
		return nullptr;
	}
	LOGFONT* lpLogFont = new LOGFONT();
	SendMessage(hWnd, WM_CHOOSEFONT_GETLOGFONT, 0, (LPARAM)lpLogFont);
	/*HDC hdc = GetDC(hWnd);
	Font.创建自逻辑字体W(hdc, lpLogFont);
	ReleaseDC(hWnd, hdc);
	_Free(lpLogFont);*/
	return lpLogFont;
}
COLORREF BlackMoonUI::zyFontDialog::取字体颜色() {
	CHOOSEFONT* pChooseFont = (CHOOSEFONT*)GetMember(L"pChooseFont");
	if (pChooseFont == nullptr) {
		return 0;
	}
	return pChooseFont->rgbColors;
}
int BlackMoonUI::zyFontDialog::取字体大小() {//单位：0.1点
	CHOOSEFONT *pChooseFont = (CHOOSEFONT*)GetMember(L"pChooseFont");
	if (pChooseFont == nullptr) {
		return 0;
	}
	return pChooseFont->iPointSize;
}
WORD BlackMoonUI::zyFontDialog::取字体类型() {//返回"字体类型_"开头常量组合
	CHOOSEFONT* pChooseFont = (CHOOSEFONT*)GetMember(L"pChooseFont");
	if (pChooseFont == nullptr) {
		return 0;
	}
	return pChooseFont->nFontType;
}
void BlackMoonUI::zyFontDialog::事件_应用按钮被单击(TRegEvent* 参数_事件指针) {
	SetMember(L"OnApply", (LONG_PTR)参数_事件指针);
}